package com.cxyw.qqrobot;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashSet;
import java.util.Set;

public class Util {

    public enum MsgType {

        PRIVATE_MESSAGE("message", "private", "私聊消息"),
        GROUP_MESSAGE("message", "group", "群聊消息");
        String postType;
        String messageType;
        String remark;

        MsgType(String postType, String messageType, String remark) {
            this.messageType = messageType;
            this.postType = postType;
            this.remark = remark;
        }

        public String getPostType() {
            return this.postType;
        }

        public String getMessageType() {
            return messageType;
        }
    }


    public enum MsgSubType {
        LEARN_DERIVATIVE("学习导数"),
        MY_DERIVATIVE("我的导数"),
        START_DERIVATIVE("开导"),
        FACE_DERIVATIVE("对导"),
        COME_ONE_SE_TU("来张涩图"),
        COME_ONE_SE_TU_R18("牛牛立正");
        String remark;

        MsgSubType(String remark) {
            this.remark = remark;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }

        public static boolean isSub(String msg) {
            for (MsgSubType value : MsgSubType.values()) {
                if (msg.contains(value.getRemark())) {
                    return true;
                }
            }
            return false;
        }
    }


    public enum Desc {
        HAS_LEARN("你已经是开导班级的一员喽!"),
        NO_LEARN("你还没有学习导数，目前开导班级中没有你的位置，请输入 学习导数 开始这门课程吧"),
        AT_NO_LEARN("对方还没有学习导数，开导都不会，让人家怎么对着你导? "),
        WILL_LEARN("在辅导员的帮助下，你已入门这项课程，你目前能坚持的开导时间为1s，积极开导能让你导数的更加持久哦~"),
        MY_DERIVATIVE("您目前能坚持的开导的时间为{}s，再接再厉哦~"),
        START_DERIVATIVE("开导成功了。同学，因手法极其熟练，你的持久开导时间努力增加了{}s，再接再厉成为开导大师吧"),
        REPEAT_DERIVATIVE("你已经萎了，目前处在连续不一定可导状态，{}s后可导"),
        FACE_WIN_DERIVATIVE("你的求导手法异常优异，对方已折服在你的手法并积分回去了。你的开导时间增加了{}s，对方的开导时间萎缩了{}s"),
        FACE_LOSE_DERIVATIVE("对方的求导手法异常优异，你已折服在对方的手法下并积分回去了。你的开导时间萎缩了{}s，对方的开导时间增加了{}s"),
        NO_AT_FACE_LOSE_DERIVATIVE("你不@人,怎么对着人家导,难道想对着空气开导?"),
        SAME_FACE_LOSE_DERIVATIVE("哼哼~，对着自己导，真是有够变态呢，牛子妹妹表示受不了并拒绝了你的自导请求"),
        REPEAT_COME_ONE_SE_TU("看jb看，你刚刚不是才看过? {}s后再来找牛子妹妹看"),
        NO_COME_ONE_SE_TU("涩图被扒光了,晚点再来找牛子妹妹看");
        String remark;

        Desc(String remark) {
            this.remark = remark;
        }

        public String getRemark() {
            return remark;
        }

        public void setRemark(String remark) {
            this.remark = remark;
        }
    }





    /**
     * 获取json字符里...key下的字符串
     */
    public static String getString(Object obj, String... key) {
        JSONObject obj1 = JSONObject.parseObject(obj.toString());
        for (int i = 0; i < key.length - 1; i++) {
            obj1 = obj1.getJSONObject(key[i]);
        }
        return obj1.getString(key[key.length - 1]);
    }

    /**
     * 获取json字符里...key下的整型
     */
    public static int getInt(Object obj, String... key) {
        JSONObject obj1 = JSONObject.parseObject(obj.toString());
        for (int i = 0; i < key.length - 1; i++) {
            obj1 = obj1.getJSONObject(key[i]);
        }
        return Integer.parseInt(obj1.getString(key[key.length - 1]));
    }

    public static Long getLong(Object obj, String... key) {
        JSONObject obj1 = JSONObject.parseObject(obj.toString());
        for (int i = 0; i < key.length - 1; i++) {
            obj1 = obj1.getJSONObject(key[i]);
        }
        String content = obj1.getString(key[key.length - 1]);
        if (null == content) {
            return null;
        }
        return Long.parseUnsignedLong(content);
    }

    /**
     * 获取响应里...key下的数组型对象
     */
    public static JSONArray getArray(Object obj, String... key) {
        JSONObject obj1 = JSONObject.parseObject(obj.toString());
        for (int i = 0; i < key.length - 1; i++) {
            obj1 = obj1.getJSONObject(key[i]);
        }
        return obj1.getJSONArray(key[key.length - 1]);
    }


    public static boolean isPrivateMsg(String msg) {
        String post_type = getString(msg, "post_type");
        String message_type = getString(msg, "message_type");
        return post_type.equals(MsgType.PRIVATE_MESSAGE.getPostType()) && message_type.equals(MsgType.PRIVATE_MESSAGE.getPostType());
    }

    public static boolean isGroupMsg(String msg) {
        String post_type = getString(msg, "post_type");
        String message_type = getString(msg, "message_type");
        return post_type.equals(MsgType.GROUP_MESSAGE.getPostType()) && message_type.equals(MsgType.GROUP_MESSAGE.getPostType());
    }


    /**
     * 获取@格式消息的内容
     */
    @Data
    @NoArgsConstructor
    public static class MsgBody {
        //如果消息里有@,那这个就是被@的qq号,无就是null;
        private Long AtQQ;
        //发送者qq
        private Long qq;
        //群ID
        private Long groupId;
        //x消息类型,group为群消息,private为私聊消息
        private String messageType;
        //发送内容
        private String content;

        public MsgBody(long qq, String content) {
            this.AtQQ = qq;
            this.content = content;
        }
    }

    //    {"time":1719069403,"self_id":3785442146,"post_type":"message","message_type":"group","sub_type":"normal","message_id":1942263934,"group_id":967428027,"peer_id":3785442146,"user_id":1124209551,"message":[{"data":{"text":"你他八的 "},"type":"text"},{"data":{"qq":"2285892725"},"type":"at"}],"raw_message":"你他八的 [CQ:at,qq=2285892725]","font":0,"sender":{"user_id":1124209551,"nickname":"V神","card":"","role":"owner","title":"","level":""}}
    public static MsgBody getMsgBody(String msg) {
        MsgBody msgBody = new MsgBody();
        String messageType = getString(msg, "message_type");
        msgBody.setMessageType(messageType);
        if (null == messageType) {
            return null;
        }
        JSONArray messageArray = getArray(msg, "message");
        if (null == messageArray) {
            return null;
        }
        msgBody.setQq(Util.getLong(msg, "user_id"));
        msgBody.setGroupId(Util.getLong(msg, "group_id"));
        for (int i = 0; i < messageArray.size(); i++) {
            JSONObject message = messageArray.getJSONObject(i);
            if (message.getString("type").equals("at")) {
                msgBody.setAtQQ(message.getJSONObject("data").getLong("qq"));
            }
            if (message.getString("type").equals("text")) {
                msgBody.setContent((null==msgBody.getContent()?"":msgBody.getContent())+message.getJSONObject("data").getString("text"));
            }
        }
        return msgBody;
    }





    public static void main(String[] args) {
        System.out.println(getString("{\"time\":1718982453,\"self_id\":3785442146,\"post_type\":\"meta_event\",\"meta_event_type\":\"lifecycle\",\"sub_type\":\"connect\",\"status\":{\"self\":{\"platform\":\"qq\",\"user_id\":3785442146},\"online\":true,\"good\":true,\"qq.status\":\"正常\"},\"interval\":15000}", "post_type"));
    }
}
